<?php
/**
* @package controller
* @file indexController.php
* @date 15.09.2009
* @version 1.0
* @copyright sample
* @author Harinath Reddy
* @todo phpDoc comments
*/
require_once 'Zend/Controller/Action.php';
class expressions_IndexController extends Cms_Controller_Action
{
	public function init()
	{
	  $this->view->siteTitle = 'Messages';
	  $this->currentPage = 1;
	  $this->resultsPerPage = 12;
			
	}
	public function indexAction()
	{
	  	
			
	}
		
	
	public function logoutAction()
	{
		if($_SESSION["sess_userid"]!="")
		{
        session_destroy();
		session_unset();
		//$msg = "Logout Successfully";
        $this->_redirect("/home/");
		}
		else
		  $this->_redirect("/home/index/inbox");
			
	}
		public function messageAction()    
	{ 
			
	}
	public function myactivityAction()
	{
	}
	public function viewAction()
	{
		$type=$this->_getParam('id');
		$currentPage=base64_decode($this->_getParam('current_page'));
		//echo $currentPage;exit;
		$id=base64_decode($this->_getParam('results_per_page'));
		//echo $id;exit;
		//echo $id;exit;
		$memid = $_SESSION["sess_userid"];
		$read_status= Doctrine::getTable('ExpressionsSent')->updateExpressionReadStatus($memid,$id,1);
		//$memid='sandhyarani.kotaru@gmail.com';
		//echo $type;exit;
		$this->view->type=$type;
		$group_type="Expressions";
		$group_details = Doctrine::getTable('Groups')->getGroupsById($memid,$group_type,1);
		//echo '<pre>';print_r(count($group_details));exit;
		$this->view->group_details= $group_details;
		if($group_details)
		{
		$this->view->count= count($group_details);
		}else
		{
			$this->view->count= 0;
		}
		if($type==1)
		{
		
		$result = Doctrine::getTable('Expressions')->getExpressionsCurrent($memid,$currentPage);
		//echo '<pre>';print_r($result);exit;
		$result['currentPage'] = $currentPage;
		//echo '<pre>';print_r($result);exit;	
		//echo $result['result'][0]['UserDetails']['photo'];exit;
		$r = Doctrine::getTable('Expressions')->getDetails($id);
		$r = Doctrine::getTable('Expressions')->getDetailsByLimit($type,currentPage);
		//echo '<pre>';print_r($r);exit;
		$this->view->details = $r;		
		}
		else if($type == 2||$type==3)
		{
			if($type == 2)
			$send_type = 1;
			else $send_type = 0;
			//echo $send_type;exit;
			$result = Doctrine::getTable('Expressions')->sentMailsCurrent($memid,$send_type,$currentPage);
			//echo '<pre>';print_r($result);exit;
			$r = Doctrine::getTable('Expressions')->getSentDetails($id,$send_type);
			$result['currentPage'] = $currentPage;
			//echo '<pre>';print_r($r);exit;
			//echo '<pre>';print_r($r['ExpressionsSent'][0]['Users'][0]['first_name']);exit;
			$this->view->details = $r;
		}
		else if($type == 4)
		{
			$result = Doctrine::getTable('Expressions')->trashMails($memid,1,12);
			$r = Doctrine::getTable('Expressions')->getTrashDetails($id);
			//echo '<pre>';print_r($r);exit;
			$this->view->details = $r;
		}
		else if($type == 5)
		{
			$result = Doctrine::getTable('Expressions')->getAnonymousMails($memid,1,12);
			$r = Doctrine::getTable('Expressions')->getAnonymousDetails($id);
			//echo '<pre>';print_r($r);exit;
			$this->view->details = $r;
		}
		else if($type == 6)
		{
			$result = Doctrine::getTable('Expressions')->getBlockedExpressions($memid,1,12);
			$r = Doctrine::getTable('Expressions')->getBlockedDetails($id);
			//echo '<pre>';print_r($r);exit;
			$this->view->details = $r;
		}
		$this->view->getExpressions = $result;
		$this->view->type = $type;
		
		$attachments = Doctrine::getTable('ExpressionsAttachements')->getAttachments($id);
		//echo '<pre>';print_r($result);exit;
		
		$this->view->total=count($attachments);
		$this->view->attachments = $attachments;
		$this->view->expr_id = $id;
				
	}
	public function viewajaxAction()
	{
		$this->_helper->layout->disableLayout();
 	    $this->getHelper('viewRenderer')->setNoRender();
		$type=$this->_getParam('id');
		$currentPage=base64_decode($this->_getParam('current_page'));
		$id=base64_decode($this->_getParam('results_per_page'));
		$memid = $_SESSION["sess_userid"];
		$read_status= Doctrine::getTable('ExpressionsSent')->updateExpressionReadStatus($memid,$id,1);
		$this->view->type=$type;
		$group_type="Expressions";
		$group_details = Doctrine::getTable('Groups')->getGroupsById($memid,$group_type,1);

		$this->view->group_details= $group_details;
		if($group_details)
		{
			$this->view->count= count($group_details);
		}else
		{
			$this->view->count= 0;
		}
		if($type==1)
		{		
			//$result = Doctrine::getTable('Expressions')->getExpressionsCurrent($memid,$currentPage);
			//$result['currentPage'] = $currentPage;
			$r = Doctrine::getTable('Expressions')->getDetails($id);
			$this->view->details = $r;		
		}
		else if($type == 2||$type==3)
		{
			if($type == 2)
			$send_type = 1;
			else $send_type = 0;
			//$result = Doctrine::getTable('Expressions')->sentMailsCurrent($memid,$send_type,$currentPage);
			$r = Doctrine::getTable('Expressions')->getSentDetails($id,$send_type);
			//$result['currentPage'] = $currentPage;
			$this->view->details = $r;
		}
		else if($type == 4)
		{
			//$result = Doctrine::getTable('Expressions')->trashMails($memid,1,12);
			$r = Doctrine::getTable('Expressions')->getTrashDetails($id);
			$this->view->details = $r;
		}
		else if($type == 5)
		{
			//$result = Doctrine::getTable('Expressions')->getAnonymousMails($memid,1,12);
			$r = Doctrine::getTable('Expressions')->getAnonymousDetails($id);
			$this->view->details = $r;
		}
		else if($type == 6)
		{
			//$result = Doctrine::getTable('Expressions')->getBlockedExpressions($memid,1,12);
			$r = Doctrine::getTable('Expressions')->getBlockedDetails($id);			
			$this->view->details = $r;
		}
		//$this->view->getExpressions = $result;
		$this->view->type = $type;
		
		$attachments = Doctrine::getTable('ExpressionsAttachements')->getAttachments($id);
		//echo '<pre>';print_r($r);exit;
		
		$this->view->total=count($attachments);
		$this->view->attachments = $attachments;
		$this->view->expr_id = $id;
		$jsonData['output'] = $this->view->render('index/viewajax.html');
		$this->_helper->json->sendJson($jsonData);
				
	}
	
	public function uploadImagesAction()
	  {
	   //echo "uploading";exit;
		  $this->_helper->layout->disableLayout();
		  $this->getHelper('viewRenderer')->setNoRender();
		  $imagesDir = '../'.$this->CONFIG->path->photosThumb;
		if(isset($_FILES['vfile']))
		  {
			 $size=$_FILES['vfile']['size'];
			 //echo $_FILES['vfile']['name'];exit;
		   	 $name = time().'_-_'.$_FILES['vfile']['name'];
			//echo $name;exit;
		 if($size>209715200)
			 {
				echo "error file size > 200 MB";
				unlink($_FILES['vfile']['tmp_name']);
				//exit;
			}
			echo $vidfile = $this->uploadFile($_FILES['vfile']['tmp_name'],$imagesDir,$name);
		}
	}
		public function uploadFile($tnm,$path,$fnm)
	 {
	 //echo "hi from  upload file method";
		   $fnm = str_replace(' ','_',$fnm);
		  if(move_uploaded_file($tnm,$path.$fnm))
		    {
			   //createFixedthumb($path.$fnm, $path.$fnm, "450", "450");
			   return $fnm;
		    }
		 else
			return "";
     }
	 
	 

	public function paginationAction()
	{
		//echo "pagination page";exit;
		$type = $this->_getParam('id') == 0? 1:$this->_getParam('id');
		$serValue = $this->_getParam('orderby') == ''? '':$this->_getParam('orderby');
		//echo $type;exit;
		$currentPage = $this->_getParam('current_page') == 0? 1:$this->_getParam('current_page');
		$resultsPerPage = $this->_getParam('results_per_page') == 0? 12:$this->_getParam('results_per_page');
		//echo $currentpage,$resultsPerPage;exit;
		$memid=$_SESSION["sess_userid"];
		$this->view->type= $type;
		$this->view->params ='';
		//$ideaDetails = Doctrine::getTable('Expressions')->getExpressions($_SESSION["sess_memberid"],$currentPage, $resultsPerPage);
		if($type == 1)
		{
			$ideaDetails = Doctrine::getTable('Expressions')->getExpressions($memid,$currentPage, $resultsPerPage,$serValue);
		} 
		else if($type == 2 || $type==3)
		{
					if($type == 2)
					$send_type = 1;
					else $send_type = 0;
					$ideaDetails = Doctrine::getTable('Expressions')->sentMails($memid,$send_type,$currentPage, $resultsPerPage);
		}
		else if($type == 4)
		{
		$ideaDetails = Doctrine::getTable('Expressions')->trashMails($memid,$currentPage, $resultsPerPage);
		}
		else if($type == 5)
		{
			$ideaDetails = Doctrine::getTable('Expressions')->getAnonymousMails($memid,$currentPage, $resultsPerPage);
		}
		else if($type == 6)
		{
			$ideaDetails = Doctrine::getTable('Expressions')->getBlockedExpressions($memid,$currentPage, $resultsPerPage);
		}
		else if($type == 7)
		{
			$ideaDetails = Doctrine::getTable('Expressions')->getGroupDetailsByid($memid,$currentPage, $resultsPerPage);
		}
		//echo '<pre>';print_r($ideaDetails);exit;
		$this->view->getExpressions = $ideaDetails;
		//echo '<pre>';print_r($ideaDetails);exit;
		$jsonData['output'] = $this->view->render('index/message.html');
		$jsonData['pages'] = $ideaDetails['pages'];
	    $jsonData['pager'] = $ideaDetails['pager'];
		 $jsonData['serValue'] = $serValue;
	    $jsonData['currentPage'] = $ideaDetails['currentPage'];
		$jsonData['resultsCount'] = $ideaDetails['resultsCount'];
		$this->_helper->json->sendJson($jsonData);
	}
	public function inboxAction() 
	{	
		$this->view->type= $this->_getParam('id');
		$result_type = $this->_getParam('current_page');
		//echo '<pre>';print_r($_POST);exit;
		if($_SESSION["sess_userid"]!="")
		{
			$this->view->inboxactive="active";
			$this->view->sentactive="";
			$this->view->draftactive="";
			$this->view->trashactive="";	
			$this->view->params ='';
			$memid = $_SESSION["sess_userid"];
			//$memid='sandhyarani.kotaru@gmail.com';
			$result = Doctrine::getTable('Expressions')->getExpressions($memid,1,12);
			//echo '<pre>';print_r($result);exit;	
			//echo $result['result'][0]['UserDetails']['photo'];exit;
			$group_type="Expressions";
			$this->view->getExpressions = $result;
			$groups = Doctrine::getTable('Groups')->getGroupsById($memid,$group_type,1);
			//echo '<pre>';print_r($groups);exit;
			$this->view->groups= $groups;
			$this->view->count= count($groups);
		}
		else 
		{
			$this->_redirect("/home/");
		}
     }
		public function composeAction() 
		{
			if($_SESSION["sess_userid"]!="")
		{
			   	$memid=$_SESSION["sess_userid"];
				
				///Get contact emails into to fieeld
				$a = Doctrine::getTable('Contacts')->getMails($memid);
				//echo '<pre>';print_r($a);exit;
				$jsonData = array();
				if(count($a)>0)
				{
					foreach($a as $key=>$mails)
					{
						$jsonData[$key]['key'] = $mails['email'];
						$jsonData[$key]['value'] = $mails['email'];					
					}
				}
				$this->_helper->json->sendJson($jsonData);
				//end
		}	
	}
	 public function preview1Action()
	{
	    $this->_helper->layout->disableLayout();
	    $colorid = $this->_getParam('id');
		//echo $colorid;exit;
		$result = Doctrine::getTable('MessageIntensity')->getColorsById($colorid);
		$this->view->result=$result;
	}

	public function saveAction()    
	{ 
		//echo '<pre>';print_r($_POST);exit;
		include('../lib/mail/SendEmail.php');
		//echo '<pre>';print_r($_POST);print_r($_FILES);exit;
		$data = $_POST;
		//echo '<pre>';print_r($data);print_r($_FILES);exit;
		if(isset($data['checkbox'])!="")
		{
			//echo "check box selected";exit;
		$anonymous=1;
		}
		else{
		$anonymous=0;
		}
		$memid=$_SESSION["sess_userid"];
		$result = Doctrine::getTable('Expressions')->addExpressions($data,$anonymous);
		if($result)
		{
		$content_type="expressions";
		$share_type=1;
		$activity = Doctrine::getTable('EmActivities')->addActivity($result,$content_type,$memid,$share_type);
		}
		//echo $activity;exit;
		foreach($data['select3'] as $email)
		{
			$receiver_id = Doctrine::getTable('Users')->getUserIdByEmail($email);
			//echo $receiver_id['first_name']." ".$_SESSION["sess_email"]."  ".$email;exit;
			if($receiver_id['user_id'])
			{
				$name=$receiver_id['first_name'];
			}
			else
			{
				$name=$email;
			}
			$is_group=0;
			$r = Doctrine::getTable('ExpressionsSent')->addExpressions($email,$result,$receiver_id['user_id'],$is_group,$memid);
			//echo '<pre>';print_r($data['sendType']);exit;
			if($r>0)
			{
				//Sending mail
				if($data['sendType']==1)
				{
				//echo $email;
				//echo '<pre>';print_r($data['sendType']);exit;
				$loginLink = $this->CONFIG->serverAddress.'home';
				$Subject = "Emotionsmirror Expressions";
				$Message = 'Hello "'.$name.'",<br/><br/>you have an Message in your emotionmirror inbox <br/><br/>
							<br/><br/><br/>Please click on the following link to login into emirror<br/><a href="'.$loginLink.'">"'.$loginLink.'"</a><br/><br/><br/>Sincerely,<br/>
								Emirror team';
				//global $serverAddress;
				sendMail($email,$Subject,$Message,$_SESSION["sess_email"]);
				}
				//end Sending of mail
			}
		}
		if($data['group'])
		{
			$is_group=1;
			$r = Doctrine::getTable('ExpressionsSent')->addGroupExpressions($result,$data['group'],$is_group,$memid);
			//echo $r;exit;
		}
		//echo '<pre>';print_r($result);exit;
		if($result)
		{
		  //echo $result;
		  $count=count($data['fileNames']);
		  //exit;
		  $i=0;
			while($i<$count)
			{
					$filename=$data['fileNames'][$i];
					//exit;
					$r = Doctrine::getTable('ExpressionsAttachements')->addAttachements($filename,$result);
					$i++;
			}
			if($data['sendType']==1)
			{
				$this->_redirect("/expressions/index/inbox/1");
			}
			else{
				$this->_redirect("/expressions/index/inbox/1");
			}
		}
		else
		{
		$this->_redirect("/Expressions/messages/compose/fail");
		}	
	}

	public function sentAction()    
	{ 
		$this->view->type= $this->_getParam('id');
		if($_SESSION["sess_userid"]!="")
		{
		$this->view->sentactive="active";
		$this->view->inboxactive="";
		$this->view->draftactive="";
		$this->view->trashactive="";
		
		$this->view->params ="";
		//$memid = $_SESSION["sess_memberid"];
		$memid=	$_SESSION["sess_userid"];
		$send_type= 1;
		$result = Doctrine::getTable('Expressions')->sentMails($memid,$send_type,1,12);
		//echo '<pre>';print_r($result);exit;	
		//echo $result['result'][0]['UserDetails']['photo'];exit;		
			$group_type="Expressions";
			$this->view->getExpressions = $result;
			$groups = Doctrine::getTable('Groups')->getGroupsById($memid,$group_type,1);
			//echo '<pre>';print_r($groups);exit;
			$this->view->groups= $groups;
			$this->view->count= count($groups);
			//$jsonData['currentPage'] = 1;
			//$this->_helper->json->sendJson($jsonData);
		$this->render('inbox');
		}
		else 
		{
		$this->_redirect("/home");
		}
			
	}
	 
	 public function draftsAction()
	 {
		$this->view->type= $this->_getParam('id');
		if($_SESSION["sess_userid"]!="")
		{
		$this->view->sentactive="";
		$this->view->inboxactive="";
		$this->view->draftactive="active";
		$this->view->trashactive="";

		$this->view->params ="";
		//$memid = $_SESSION["sess_memberid"];
		$memid=$_SESSION["sess_userid"];
		$send_type= 0;
		$result = Doctrine::getTable('Expressions')->sentMails($memid,$send_type,1,12);
		//echo '<pre>';print_r($result);exit;	
		//echo $result['result'][0]['UserDetails']['photo'];exit;		
			$group_type="Expressions";
			$this->view->getExpressions = $result;
			$groups = Doctrine::getTable('Groups')->getGroupsById($memid,$group_type,1);
			//echo '<pre>';print_r(count($groups));exit;
		$this->view->groups= $groups;
		$this->view->count= count($groups);
		$this->render('inbox');
    	}
		else 
		{
			$this->_redirect("/home/index/index/logout");
		}
	}
		public function trashAction()
	 {
		$this->view->type= $this->_getParam('id');
		if($_SESSION["sess_userid"]!="")
		{
		    $this->view->sentactive="";
			$this->view->inboxactive="";
		   $this->view->draftactive="";
		   $this->view->trashactive="active";
	
		//$this->view->message = $message;
		$this->view->params ="";
			//$memid = $_SESSION["sess_memberid"];
			$memid=$_SESSION["sess_userid"];
			$result = Doctrine::getTable('Expressions')->trashMails($memid,1,12);
		//echo '<pre>';print_r($result);exit;	
		//echo $result['result'][0]['UserDetails']['photo'];exit;		
			$group_type="Expressions";
			$this->view->getExpressions = $result;
			$groups = Doctrine::getTable('Groups')->getGroupsById($memid,$group_type,1);
			//echo '<pre>';print_r(count($groups));exit;
		$this->view->groups= $groups;
		$this->view->count= count($groups);
		$this->render('inbox');
    	}
		else 
		{
			$this->_redirect("/home/index/index/logout");
		}
	}
	public function anonymousAction()
	{
		$this->view->type= 5;
		$this->view->params ="";
		$memid = $_SESSION["sess_userid"];
		//echo $memid;exit;
		$result = Doctrine::getTable('Expressions')->getAnonymousMails($memid,1,12);
		//echo '<pre>';print_r($result);exit;
		$showAnonymous = Doctrine::getTable('Expressions')->getAnonymousExpressions();
		//echo '<pre>';print_r($showAnonymous);exit;
		$count=count($showAnonymous);
		$this->view->count = $count;
		$this->view->getExpressions = $result;
	  	//echo '<pre>';print_r($result);exit;
			
	}
	public function blockAction()
	{
		$this->view->type= 6;
		$this->view->params ="";
		$userid = $_SESSION["sess_userid"];
		//echo $memid;exit;
		$result = Doctrine::getTable('Expressions')->getBlockedExpressions($userid,1,12);
		//echo '<pre>';print_r($result);exit;
		$this->view->getExpressions = $result;
	  	
			
	}
	public function closeAction()
	{
			$type= $this->_getParam('id');
			//echo $type;exit;
			$this->view->type=$type;
			$result_type = $this->_getParam('current_page');
			//echo $result_type;exit;
			 if($result_type == "close")
				{
					$id = base64_decode($this->_getParam('results_per_page'));
					//echo $id;exit;
					if($type == 1)
					{
					$status="RDelete";
					$r2 = Doctrine::getTable('ExpressionsSent')->deleteExpression($_SESSION['sess_userid'],$status,$id);
					$this->_redirect("/expressions/index/inbox/1");
					}
					else if($type == 2 || $type == 3)
					{
					$status="SDelete";
					//echo $status;exit;
					$r2 = Doctrine::getTable('ExpressionsSent')->deleteSentExpression($_SESSION['sess_userid'],$status,$id);
					if($type==2)
					$this->_redirect("/expressions/index/sent/".$type);
					else $this->_redirect("/expressions/index/drafts/".$type);
					}
					else if($type == 4)
					{
					$status="Delete";
					$r2 = Doctrine::getTable('ExpressionsSent')->deleteExpression($_SESSION['sess_userid'],$status,$id);
					$this->_redirect("/expressions/index/trash/4");
					} else {
					
					}
			}
		}
		function changeStatusAction()
		{
					//echo '<pre>';print_r($_POST);exit;
					$id = $_POST['val'];
					$action = $_POST['tpye'];
					$type = $_POST['msgType'];
					
					if($action == 'close')
					{
						if($type == 1)
						{
						//echo 'sdfsdf';exit;
						$status="RDelete";
						$r2 = Doctrine::getTable('ExpressionsSent')->deleteExpression($_SESSION['sess_userid'],$status,$id);
						}
						else if($type == 2 || $type == 3)
						{
						$status="SDelete";
						//echo $status;exit;
						$r2 = Doctrine::getTable('ExpressionsSent')->deleteSentExpression($_SESSION['sess_userid'],$status,$id);
						
						//if($type==2)
						//$this->_redirect("/expressions/index/sent/".$type);
						//else $this->_redirect("/expressions/index/drafts/".$type);
						}
						else if($type == 4)
						{
						$status="Delete";
						//echo '<pre>';print_r($status);exit;
						$r2 = Doctrine::getTable('ExpressionsSent')->deleteExpression($_SESSION['sess_userid'],$status,$id);
						//$this->_redirect("/expressions/index/trash/4");
						}
						else if($type == 5)
						{
						$status="RDelete";
						$r2 = Doctrine::getTable('ExpressionsSent')->deleteExpression($_SESSION['sess_userid'],$status,$id);
						//$this->_redirect("/expressions/index/trash/4");
						}
						else if($type == 6)
						{
						$status="Rdelete";
						//echo $status;exit;
						$r2 = Doctrine::getTable('ExpressionsSent')->deleteExpression($_SESSION['sess_userid'],$status,$id);
						//$this->_redirect("/expressions/index/trash/4");
						}
					
					} else if($action == 'block'){
						$blockid = Doctrine::getTable('MemberStatus')->getBlockId($_SESSION['sess_userid'],$id);
						//echo '<pre>';print_r(count($blockid));exit;
						if(count($blockid)!=0)
						{
							$status="block";
							$r2 = Doctrine::getTable('MemberStatus')->unBlockUserIds($_SESSION['sess_userid'],$status,$id);
						}
						else
						{
						$r2 = Doctrine::getTable('MemberStatus')->blockUser($_SESSION['sess_userid'],$id);
						}
						//$r2 = Doctrine::getTable('ExpressionsSent')->setstatusInactive($_SESSION['sess_userid'],$id);
						//$this->_redirect("/expressions/index/inbox/1/success");	
					}
					$jsonData['output'] = $r2;
					if(isset($_POST['view']))
					{
						$jsonData['view'] = $type;
					} else {
						$jsonData['view'] = 0;
					}
					$this->_helper->json->sendJson($jsonData);
					//$this->paginationAction();

						
		}
	public function addtogroupAction()
	{
		//echo '<pre>';print_r($_POST);exit;
		$id = $_POST['val'];
		$type = $_POST['userId'];
		$result = Doctrine::getTable('GroupDetails')->getGroupDetailsByUserId($id,$type);
		//echo '<pre>';print_r(count($result));exit;
		if(count($result)==0)
		{
		$user_email = Doctrine::getTable('Users')->getEmail($type);
		$r2 = Doctrine::getTable('GroupDetails')->addDetailsToGroup($id,$type,$user_email);
		$jsonData['output'] = 1;
		}else
		{
		$jsonData['output'] = 0;
		}
		$this->_helper->json->sendJson($jsonData);
	
	}
	public function composenewAction()
	{
		$memid = $_SESSION["sess_userid"];
		$this->_helper->layout->disableLayout();
		$group_type="Expressions";
			$groups = Doctrine::getTable('Groups')->getGroupsById($memid,$group_type,1);
			//echo '<pre>';print_r($groups);exit;
			$this->view->groups= $groups;
			$this->view->count= count($groups);
	}
	public function replyAction()
	{
		$this->_helper->layout->disableLayout();
		//echo $_POST;exit;
		$group_name=0;
		$expr_id = $this->_getParam("val");
		$receiver_email = $this->_getParam("email");
		$type = $this->_getParam("type");
		$memid = $_SESSION["sess_userid"];
		//echo $type;exit;
		if($type=="draft")
		{
				//echo $type;exit;
				$id = $expr_id;
				$memid = $_SESSION["sess_userid"];
				$send_type=0;
				$r = Doctrine::getTable('Expressions')->getSentDetails($id,$send_type);
				//echo count($r['ExpressionsSent']);exit;
				//echo '<pre>';print_r($r);exit;
				$send_type=1;
				//$result = Doctrine::getTable('Expressions')->changeSendTypeExpression($id,$send_type);
				//echo $result;exit;
				$details['subject']=$r['subject'];
				$details['message']=$r['message'];
				foreach($r['ExpressionsSent'] as $ExpressionsSent)
				{
					if($ExpressionsSent['is_group']==0)
					{
					$receiver_email[] =$ExpressionsSent['receiver_email'];
					$group_id=0;
					
					}
					
					else
					{
						$group_id=$ExpressionsSent['receiver_id'];
						$group_type="Expressions";
						$group_name=Doctrine::getTable('Groups')->getGroupDetailsByGroupId($group_id,$group_type);
						//echo '<pre>';print_r($name);exit;
					}
					
				}
				$attachment_name = array();
				foreach($r['ExpressionsAttachements'] as $ExpressionsAttachements)
				{
					if($ExpressionsAttachements['attachments']!="")
					{
						$attachment_id[]=$ExpressionsAttachements['id'];
						$attachment_name[]=$ExpressionsAttachements['attachments'];
					}
					
				}
				//echo '<pre>';print_r($receiver_id);print_r($group_id);print_r($group_name);print_r($attachment_name);exit;
			$this->view->group_id= $group_id;
			$this->view->expr_id=$id;
			$this->view->anonymous=$r['anonymous'];
			$this->view->attachment_name= $attachment_name;
			$this->view->type="drafts";
			$this->view->email=$receiver_email;
		}
		else
		{
		$details = Doctrine::getTable('Expressions')->getExpressionDetails($expr_id);
		
		//echo '<pre>';print_r($details);exit;
		//echo $receiver_email;exit;
		$this->view->expr_id=$expr_id;
		$this->view->type="";
		$this->view->email=$receiver_email;
		$this->view->group_id= 0;
		}
		$group_type="Expressions";
		$groups = Doctrine::getTable('Groups')->getGroupsById($memid,$group_type,1);
		$this->view->expr_details=$details;
		
		
		//$this->view->group_id= $group_id;
		//$this->view->anonymous=$r['anonymous'];
		//echo '<pre>';print_r($groups);exit;
		$this->view->groups= $groups;
		$this->view->count= count($groups);
		
	
	}
	public function changegroupStatusAction()
	{
		$status = $_POST['val'];
		$group_ids = trim($_POST['values'],',');
		if($group_ids!='')
		{
			$memid = $_SESSION["sess_userid"];
			$result = Doctrine::getTable('Groups')->changeStatusGroupId($memid,$status,$group_ids);
			$jsonData['output'] = $result;
			$this->_helper->json->sendJson($jsonData);
		}
	
	}
	public function deleteAnonymousAction()
	{
		//echo '<pre>';print_r($_POST);exit;
		$status = $_POST['val'];
		$expr_ids = trim($_POST['values'],',');
		if($expr_ids!='')
		{
			$memid = $_SESSION["sess_userid"];
			$result = Doctrine::getTable('ExpressionsSent')->deleteAnonymous($status,$expr_ids);
			$jsonData['output'] = $result;
			$this->_helper->json->sendJson($jsonData);
		}
	
	}
	public function unblockUsersAction()
	{
		//echo '<pre>';print_r($_POST);exit;
		$status = $_POST['val'];
		$block_ids = trim($_POST['values'],',');
		if($block_ids!='')
		{
			$memid = $_SESSION["sess_userid"];
			$result = Doctrine::getTable('MemberStatus')->unBlockUserIds($memid,$status,$block_ids);
			$jsonData['output'] = $result;
			$this->_helper->json->sendJson($jsonData);
		}
	
	}
	public function addgroupAction()
	{
	    //echo '<pre>';print_r($_POST);print_r($_FILES);exit;
			$name = $_POST['Name'];
			$desc = $_POST['message'];
			$size = $_FILES['size'];
			$group_type="Triggers";
			$imagesDir = '../'.$this->CONFIG->path->groupLogos;
			if($_FILES['file']['name']!="")
			{
				
				if($size>300000)
				{
				echo "error file size > 3 MB";
				unlink($_FILES['file']['tmp_name']);
				exit;
				}
				$fileName = str_replace(' ', '_', $_FILES['file']['name']);
				$fsname=date('Ymdhis').'_-_'.$fileName;
				//echo $fsname;exit;
				move_uploaded_file($_FILES['file']['tmp_name'],$imagesDir.$fsname);				
			}
		//echo $name,$desc;exit;
		$memid = $_SESSION["sess_userid"];
		$result = Doctrine::getTable('Groups')->getGroupName($memid,$name,$group_type);
		//echo $result[numCount]);exit;
		if($result[numCount]==0)
		{
		
		$r2 = Doctrine::getTable('Groups')->addGroup($memid,$name,$desc,$fsname,$group_type);
		//echo $r2;exit;
		$this->_redirect("/contacts/index/groups/");
		//$this->_redirect("/expressions/index/groups/7");
		}
	}
	public function updategroupAction()
	{
	    //echo '<pre>';print_r($_POST);print_r($_FILES);exit;
			$memid = $_SESSION["sess_userid"];
			$group_id=$this->_getParam('id');
			$name = $_POST['groupname'];
			$desc = $_POST['messageText'];
			$size = $_FILES['size'];
			$imagesDir = '../'.$this->CONFIG->path->groupLogos;
			if($_FILES['file']['name']!="")
			{
				
				if($size>300000)
				{
				echo "error file size > 3 MB";
				unlink($_FILES['file']['tmp_name']);
				exit;
				}
				$fileName = str_replace(' ', '_', $_FILES['file']['name']);
				$fsname=date('Ymdhis').'_-_'.$fileName;
				//echo $fsname;exit;
				move_uploaded_file($_FILES['file']['tmp_name'],$imagesDir.$fsname);	
			$r2 = Doctrine::getTable('Groups')->updateGroup($memid,$name,$desc,$fsname,$group_id);
			//echo $r2;exit;
							
			}
			else
			{
				$r2 = Doctrine::getTable('Groups')->updateGroups($memid,$name,$desc,$group_id);
			}
			$this->_redirect("/expressions/index/groups/7");
		//echo $name,$desc;exit;
		
		
			
	}
	public function removeAction()
	{
		$this->_helper->layout->disableLayout();
		$rows=0;
		//$videosTemp= '../'.$this->CONFIG->path->contentTempFiles;
		 $videosTemp = '../'.$this->CONFIG->path->photosThumb;
		$videoFile = trim($_GET['filename']);
		@unlink($videosTemp.$videoFile);
		$jsonData['rows']  = $rows;
		$this->_helper->json->sendJson($jsonData);

	}	
	public function groupsAction()
	{
		//echo $_POST;exit;
		$this->view->type= 7;
		$message=$this->_getParam('current_page');
		//echo $message;exit;
			if($message == 'success')
			{
			$this->view->message="Group added successfully.";
			}
			else if($message == 'fail')
			{
				$this->view->message="Group Already Exist/fail";
			} else {
				$this->view->message="";
			}
		$memid = $_SESSION["sess_userid"];
		//echo $memid;exit;
		$group_type="Expressions";
		$result = Doctrine::getTable('Groups')->getGroupDetails($memid,$group_type,$currentPage,$resultPerPage);
		//echo '<pre>';print_r($result);exit;
		$count=count($result['result']);
		$this->view->total=$count;
		$this->view->result=$result;
			
	}	
	public function groupviewAction()
	{
		$group_id=base64_decode($this->_getParam('id'));
		//echo $group_id;exit;
		$memid = $_SESSION["sess_userid"];
		//echo $memid;exit;
		$groupName = Doctrine::getTable('Groups')->getGroupsById($group_id);
		$result = Doctrine::getTable('GroupDetails')->getGroupDetailsByGroupId($group_id);
		//echo '<pre>';print_r($result);exit;
		$count=count($result);
		$this->view->count=$count;
		$this->view->gname=$groupName['name'];
		$this->view->group_id=$group_id;
		$this->view->result=$result;
			
	}	
	public function editgroupAction()
	{
		$this->_helper->layout->disableLayout();
		$group_id=$this->_getParam("val");
		//echo $group_id;exit;
		$memid = $_SESSION["sess_userid"];
		$result = Doctrine::getTable('Groups')->getGroupDetailsByid($memid,$group_id);
		//echo '<pre>';print_r($result);exit;
			$this->view->result=$result;
			$this->view->group_id=$group_id;
	}	
	public function editgroupdetailsAction()
	{
		$this->_helper->layout->disableLayout();
		$id=$this->_getParam("val");
		$group_id=$this->_getParam("group_id");
		//echo $group_id;exit;
		$memid = $_SESSION["sess_userid"];
		$result = Doctrine::getTable('GroupDetails')->getDetailsByid($id);
		//echo '<pre>';print_r($result);exit;
		$this->view->id = $id;
		$this->view->group_id = $group_id;
		$this->view->result=$result;
	}	
	public function updategroupdetailsAction()
	{
		$id=$this->_getParam("id");
		$group_id=$this->_getParam('current_page');
		//echo $group_id;exit;
		$name=$_POST['name'];
		$email=$_POST['email'];
		$memid = $_SESSION["sess_userid"];
		$result = Doctrine::getTable('GroupDetails')->updateGroupDetails($id,$name,$email);
		//echo '<pre>';print_r($result);exit;
		$this->_redirect("expressions/index/groupview/".$group_id);
	}	
	public function checkaddgroupAction()
	{
		//$getAll=Doctrine::getTable('ExpressionsSent')->getMails($memid);
		//$this->view->all = $getAll;
		 $this->view->render('/index/message1.html');
		
			
	}	
	public function removeRecordAction()
	{
		$id = $_POST['val'];
		$memid = $_SESSION["sess_userid"];
		//echo $memid,$id;exit;
		$result = Doctrine::getTable('GroupDetails')->removeRecord($memid,$id);
		//echo '<pre>';print_r($result);exit;
		$jsonData['output'] = 1;
		$this->_helper->json->sendJson($jsonData);
			
	}
	public function sendDraftsAction()
	{
		//echo '<pre>';print_r($_POST);exit;
		include('../lib/mail/SendEmail.php');
		$id = $this->_getParam('id');
		$data = $_POST;
		//echo '<pre>';print_r($data);print_r($_FILES);exit;
		if(isset($data['checkbox'])!="")
		{
			$anonymous=1;
		}
		else{
		$anonymous=0;
		}
		//echo $anonymous;exit;
		$memid=$_SESSION["sess_userid"];
		$result_delete = Doctrine::getTable('ExpressionsSent')->deleteExpressionsByExpressionId($id);
		$result_change = Doctrine::getTable('Expressions')->changeSendTypeExpression($id,$_POST);
		foreach($data['select3'] as $email)
		{
			$receiver_id = Doctrine::getTable('Users')->getUserIdByEmail($email);
			//echo $receiver_id['first_name']." ".$_SESSION["sess_email"]."  ".$email;exit;
			if($receiver_id['user_id'])
			{
				$name=$receiver_id['first_name'];
			}
			else
			{
				$name=$email;
			}
			$is_group=0;
			$r = Doctrine::getTable('ExpressionsSent')->addExpressions($email,$id,$receiver_id['user_id'],$is_group,$memid);
			//echo '<pre>';print_r($data['sendType']);exit;
			if($r>0)
			{
				//Sending mail
				if($data['sendType']==1)
				{
				//echo '<pre>';print_r($data['sendType']);exit;
				$loginLink = $this->CONFIG->serverAddress.'home';
				$Subject = "Emotionsmirror Expressions";
				$Message = 'Hello "'.$name.'",<br/><br/>you have an Message in your emotionmirror inbox <br/><br/>
							<br/><br/><br/>Please click on the following link to login into emirror<br/><a href="'.$loginLink.'">"'.$loginLink.'"</a><br/><br/><br/>Sincerely,<br/>
								Emirror team';
				//global $serverAddress;
				sendMail($email,$Subject,$Message,$_SESSION["sess_email"]);
				}
				//end Sending of mail
			}
		}
		if($data['group'])
		{
			$is_group=1;
			$r = Doctrine::getTable('ExpressionsSent')->addGroupExpressions($id,$data['group'],$is_group,$memid);
			//echo $r;exit;
		}
		//echo '<pre>';print_r($result);exit;
		if($r>0)
		{
		  //echo $result;
		  $count=count($data['fileNames']);
		  //exit;
		  $i=0;
			while($i<$count)
			{
					$filename=$data['fileNames'][$i];
					//exit;
					$r = Doctrine::getTable('ExpressionsAttachements')->addAttachements($filename,$id);
					$i++;
			}
			if($data['sendType']==1)
			{
				$this->_redirect("/expressions/index/inbox/1");
			}
			else{
				$this->_redirect("/expressions/index/inbox/1");
			}
		}
		
			
	}
	public function anonymousStatusAction()
	{
		$this->view->type= 5;
		$status = $_POST['status'];
		$memid = $_SESSION["sess_userid"];
		//echo $memid;exit;
		if($status=="yes")
		{
			$result = Doctrine::getTable('Expressions')->getAnonymousExpressions();
			foreach($result as $result)
			{
				//echo '<pre>';print_r($result['id']);exit;
				$status = Doctrine::getTable('Expressions')->changeShowAnonymousStatus($result['id'],$status);
			}
		}
		if($status=="no")
		{
			$result = Doctrine::getTable('Expressions')->getAnonymousExpressionsByUserId();
			foreach($result as $result)
			{
				//echo '<pre>';print_r($result['id']);exit;
				$status = Doctrine::getTable('Expressions')->changeShowAnonymousStatus($result['id'],$status);
			}
		}
		$jsonData['output'] = $status;
		$this->_helper->json->sendJson($jsonData);
			
	}
	

}	
